var item = sub
var table = document.getElementById('table')
var money_type = new Object()
var money_subtype = new Object()

$.ajax({
    url : "/money/api/type",
    async: false,
    success : function(data) {
        for (i=0; i<data.length; i++) {
            money_type[data[i].english] = data[i].chinese
            ele = `<option value="`+(data[i].english)+`">`+data[i].chinese+`</option>`
            document.getElementById("type_select").innerHTML += ele
            document.getElementById("post_type_select").innerHTML += ele
        }
    }
})
$.ajax({
    url : "/money/api/subtype",
    async: false,
    success : function(data) {
        for (i=0; i<data.length; i++) {
            money_subtype[data[i].english] = data[i].chinese
        }
    }
})
searchMoneyGet("*","*")
$.get("/money/api/sum",function(data) {
    document.getElementById("sum").innerHTML = data.sum / 100
})

// 加入用户列表到用户选择框
// 用户列表在 /static/common/util.js 中已经统一获取
for  (let element  in sysuser) {
    ele = `<option value="`+(sysuser[element].uid)+`">`+sysuser[element].realname+`</option>`
    document.getElementById("post_user_select").innerHTML += ele
};


function searchMoney() {
    subtype = document.getElementById('subtype_select').value
    type = document.getElementById('type_select').value
    searchMoneyGet(subtype,type)
}
function searchMoneyGet(subtype,type) {
    $.get("/money/api/record",{
        subtype: subtype,
        type: type
    },function(data) {
        table.innerHTML = ''
        for (i=0; i<data.length; i++) {
            d = data[i]
            table.innerHTML += item
                                .replace("ID",d.id)
                                .replace("CONTENT",d.content)
                                .replace("USER",sysuser[d.user].realname)
                                .replace("TIME",unix2time(d.time))
                                .replace("SUBTYPE",money_subtype[d.subtype])
                                .replace("TYPE",money_type[d.type])
                                .replace("NUMBER",d.number/100)
        }
    })
}

var post_dialog = new mdui.Dialog("#post_dialog");
// 上传
function addNew() {
    subtype = document.getElementById('post_subtype_select').value
    type = document.getElementById('post_type_select').value
    content = document.getElementById('post_content').value
    number = document.getElementById('post_number').value
    user = document.getElementById('post_user_select').value

    // 检查
    if (subtype == '*') {
        error_bar('请选择收支状态！')
        return
    }
    if (type == '*') {
        error_bar('请选择类型（新建类型暂时需要联系管理员）')
        return
    }
    if (user == "0") {
        error_bar('请选择操作人')
        return
    }
    if (!validateMoney(number)) {
        error_bar('金额的单位应该是元吧')
        return
    }
    if (content.length == 0 || content.length >100) {
        error_bar('备注需要保证在1到100字')
        return
    }
    $.post('/money/api/post',{
        subtype:subtype,
        type:type,
        number:number,
        content:content,
        user:user
    },function(data) {
        if (data.code != 0) {
            error_bar(data.msg)
            return
        }
        post_dialog.close()
        searchMoney()
    })
}
